import 'package:dio/dio.dart';
import 'package:flutter_ahj_app/app/HttpRequest.dart';
import 'package:flutter_ahj_app/app/changeDefaultPwd/BaseData.dart';
import 'package:flutter_ahj_app/base/basewidget/model/BaseModel.dart';
import 'package:flutter_ahj_app/base/bean/GYSData.dart';
import 'package:rxdart/rxdart.dart';
import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';

class LoginModel extends BaseModel {
  BehaviorSubject<Map> _dataObservable;

  BehaviorSubject<String> _errorbservable;

  Stream<Map> get dataStream => _dataObservable.stream;

  Stream<String> get errorStream => _errorbservable.stream;

/*
name 用户名
Password 密码
* */
  getTestData(String name, String password) {
    _dataObservable = BehaviorSubject();
    _errorbservable = BehaviorSubject();
    HttpRequest.userlogIn(name, password).listen((request) {
      //接口请求成功 （request为map） 自己设置解析数据
      _dataObservable.add(request);
    }, onError: (error) {
      try {
        if(error.response.statusCode==401){
          _dataObservable.add(error.response.data);
        }else{
          _dataObservable.add(null);
          errorData.add(error);
        }


      } catch (e) {
        _dataObservable.add(null);
      }
    });
  }

  // 判断是不是供应商
  BehaviorSubject<GYSInfo> gysInfoObservable;

  Stream<GYSInfo> get gysInfoStream => gysInfoObservable.stream;

  gysInfo() {
    gysInfoObservable = BehaviorSubject();
    HttpRequest.gysInfo().listen((request) {
      //接口请求成功 （request为map） 自己设置解析数据
      GYSData gys = GYSData.fromJson(request);
      if (gys.code != 0 || gys.data == null) {
        gysInfoObservable.add(null);
      } else {
        gysInfoObservable.add(gys.data);
      }
    }, onError: (e) {
      //请求异常等
      errorData.add(e);
    });
  }


  // 获取用户角色
  BehaviorSubject<int> getUserRoleObservable;

  Stream<int> get getUserRoleStream => getUserRoleObservable.stream;

  getUserRoleInfo() {
    getUserRoleObservable = BehaviorSubject();
    HttpRequest.getUserRole().listen((request) {
      //接口请求成功 （request为map） 自己设置解析数据

      if (request["code"] != 0) {
        getUserRoleObservable.add(1);
      } else {
        getUserRoleObservable.add(request["data"]["role"]);
      }
    }, onError: (e) {
      //请求异常等
      errorData.add(e);
    });
  }


  getCheckToken() {
    var map = {"token": UserInfoUtils.getUserToken()};
    HttpRequest.getCheckToken(map).listen((request) {
      print(request);
    }, onError: (e) {
      //请求异常等
      errorData.add(e);
    });
  }

  void dispose() {
    if (gysInfoObservable != null) {
      gysInfoObservable.close();
    }
    if (_dataObservable != null) {
      _dataObservable.close();
    }
    if (_errorbservable != null) {
      _errorbservable.close();
    }
    if (_roleObservable != null) _roleObservable.close();
    if (_getCheckCodeObservable != null) _getCheckCodeObservable.close();
    if (_checkCodeLoginObservable != null) _checkCodeLoginObservable.close();
    super.dispose();
  }

  BehaviorSubject _roleObservable;

  Stream get roleStream => _roleObservable.stream;

  changeRoles() {
    _roleObservable = new BehaviorSubject();
    // 固定传2
    HttpRequest.usersRole(2).listen((result) {
      _roleObservable.add(result);
    }, onError: (e) {
      _roleObservable.add(null);
      errorData.add(e);
    });
  }

  BehaviorSubject _getCheckCodeObservable;

  Stream get checkCodeStream => _getCheckCodeObservable.stream;

  getCheckCode(String phone) {
    _getCheckCodeObservable = new BehaviorSubject();
    HttpRequest.getCheckCode(phone).listen((result) {
      _getCheckCodeObservable.add(result);
    }, onError: (e) {
      DioError error = e;
      try {
        BaseData baseData = BaseData.fromJson(error.response.data);
        baseData.code = error.response.data["code"];
        baseData.msg = error.response.data["msg"];
        _getCheckCodeObservable.add(baseData);
      } catch (e) {
        _getCheckCodeObservable.add(null);
      }
    });
  }

  BehaviorSubject<Map> _checkCodeLoginObservable;

  Stream<Map> get checkCodeLoginStream =>
      _checkCodeLoginObservable.stream;

  checkCodeLogin(String phone, String code) {
    _checkCodeLoginObservable = new BehaviorSubject();

    HttpRequest.checkCodeLogin(phone, code).listen((result) {
      _checkCodeLoginObservable.add(result);
    }, onError: (e) {
      DioError error = e;
      try {
        _checkCodeLoginObservable.add(error.response.data);
      } catch (e) {
        _checkCodeLoginObservable.add(null);
      }
    });
  }

  BehaviorSubject<Map> _yiJianLoginObservable;

  Stream<Map> get yiJianLoginStream =>
      _yiJianLoginObservable.stream;

  yiJianLogin(String code) {
    _yiJianLoginObservable = new BehaviorSubject();

    HttpRequest.yiJianLogin(code).listen((result) {
      _yiJianLoginObservable.add(result);
    }, onError: (e) {
      DioError error = e;
      try {
        _yiJianLoginObservable.add(error.response.data);
      } catch (e) {
        _yiJianLoginObservable.add(null);
      }
    });
  }
}
